package main

import (
	"fmt"
)

func main() {
	channelDemo()
}

type work struct {
	in   chan int
	done chan bool
}

func worker(id int, work work) {
	for v := range work.in {
		fmt.Printf("Worked %d received %c \n", id, v)
		work.done <- true
	}

}
func createWorker(id int) work {
	v := work{
		in:   make(chan int),
		done: make(chan bool),
	}
	go worker(id, v)
	return v
}
func channelDemo() {
	/* [01] 创建channel数组 */
	var channels [10]work
	for i := 0; i < 10; i++ {
		channels[i] = createWorker(i)
	}
	/* [02] 给每个channel分配任务 */
	//for i:=0;i<10;i++ {
	//	channels[i].in<-'a'+i
	//	<- channels[i].done
	//}
	for i, v := range channels {
		v.in <- 'a' + i
	}
	///* [03] 让出CPU时间切片 */
	//time.Sleep(time.Second)
	for _, v := range channels {
		<-v.done
	}

}

/*
	1.创建10个worker处理问题
*/
